package tt.dz.egobus.servicemanager.business.grab;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tt.dz.egobus.api.exception.ResponseException;
import tt.dz.egobus.api.managerservice.manager.RequestTask;
import tt.dz.egobus.api.managerservice.manager.UserRequestTypeInfo;
import tt.dz.egobus.bean.BusinessStatus;
import tt.dz.egobus.servicemanager.bean.AbstractGrabApplicationContext;
import tt.dz.egobus.servicemanager.business.point.BusPoint;
import tt.dz.egobus.servicemanager.core.exception.BusinessException;
import tt.dz.egobus.servicemanager.entities.DriverRegisterBusRelative;
import tt.dz.egobus.servicemanager.service.BusinessCacheService;
import tt.dz.egobus.servicemanager.service.BusinessServiceService;
import tt.dz.egobus.servicemanager.service.DriverRegisterBusRelativeService;
import tt.dz.egobus.servicemanager.service.GrabRedisBusinessWorkCacheService;
import tt.dz.egobus.servicemanager.vo.BusinessRequestVo;

import static tt.dz.egobus.bean.ServiceManagerI18NCodeEnum.RPC_DRIVER_GRAB_FAIL;

/**
 * @author Anson
 * @date 17-8-29
 * @name 司机及时抢单模式
 * @since 1.0.7
 */
public class RightNowGrabBusiness extends AbstractGrabApplicationContext implements GrabBusinessContext {

    private static final Logger LOGGER = LoggerFactory.getLogger(RightNowGrabBusiness.class);

    private final DriverRegisterBusRelative relative;
    private final BusPoint busPoint;

    public RightNowGrabBusiness(DriverRegisterBusRelative relative, BusPoint busPoint) {
        this.relative = relative;
        this.busPoint = busPoint;
    }


    @Override
    public UserRequestTypeInfo.UserRequestType getRequestType() {
        return UserRequestTypeInfo.UserRequestType.NOW;
    }

    @Override
    public boolean handleGrabBusiness(BusinessRequestVo businessRequestVo, String businessId, String driverId) throws BusinessException {
        GrabRedisBusinessWorkCacheService grabBusinessWorkCacheService = getGrabRedisBusinessWorkCacheService();
        DriverRegisterBusRelativeService driverRegisterBusRelativeService = this.getDriverRegisterBusRelativeServiceReference();
        BusinessServiceService businessServiceService = this.getBusinessServiceServiceReference();
        BusinessCacheService businessCacheService = this.getBusinessCacheServiceReference();
        RequestTask requestTask = businessRequestVo;
        String userId = requestTask.getUserId();
        boolean isDone = false;
        if (businessServiceService.getBusinessByBusinessId(businessId) != null) {
            throw new ResponseException(RPC_DRIVER_GRAB_FAIL);
        }
        //创建一个业务流水号
        try {
            isDone = businessServiceService.doSaveBeginBusiness(relative,
                    businessId, userId, busPoint, businessRequestVo);
        } catch (Exception e) {
            isDone = false;
        }
        if (!isDone) {
            //事务回滚
            throw new ResponseException(RPC_DRIVER_GRAB_FAIL);
        }
        //更新当前司机绑定车辆状态
        isDone = driverRegisterBusRelativeService.doUpdateBeginBusiness(relative, businessId);
        LOGGER.info("[及时包车]抢单模式,司机抢单->当前更新当前司机绑定车辆状态={}", isDone);
        if (!isDone) {
            //事务回滚
            throw new ResponseException(RPC_DRIVER_GRAB_FAIL);
        }
        //更新当前司机的状态,修改当前用户的开始行程状态
        isDone = grabBusinessWorkCacheService.doDriverBeginBusiness(driverId);
        if (!isDone) {
            //抢单失败
            throw new ResponseException(RPC_DRIVER_GRAB_FAIL);
        }
        LOGGER.info("[及时包车]调用者确认某个司机进行派单分发->更新当前司机的状态为={}", isDone);
        //若是当前的处理结果成功，则确认当前分配的结果正确性
        //则在缓存业务中需要更新当前业务状态为开始形成的状态
        BusinessStatus beginStatus = BusinessStatus.BEGIN_BUSINESS;
        LOGGER.info("[及时包车]抢单模式,司机抢单,确认某个司机进行派单分发->当前业务={},修改当前业务状态为={}", businessId, beginStatus.getCode());
        businessCacheService.updateStatus(businessId, beginStatus);
        return isDone;
    }
}
